package com.sapp.imagecolorization.utils

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat

class RuntimePermission {

    private fun hasPermissions(context: Context, vararg permissions: String): Boolean =
        permissions.all {
            ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
        }

    fun checkPermission(
        context: Context,
        permissions: Array<out String>,
        requestId: Int,
        check: () -> Unit
    ) {
        if (!hasPermissions(context, *permissions)) {
            ActivityCompat.requestPermissions(
                context as Activity,
                permissions,
                requestId
            )
        } else {
            check()
        }
    }

}